package ChatNI;

import ChatNI.ChatNI;
import java.io.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import ourMessage.FilePart;

/**
 * Classe pour la reception de fichier.
 *
 * @author Ridiss
 */
public class FileReceiver extends Thread {
    
       
        private int port;
        private ChatNI chatnet;
        private String ip;
        private Socket clientSocket;
        private InputStream reader;
        private File fichier;
        private FileOutputStream fos;
        private String filname;
        private long sizeFile;


    /**
     *Constructeur Pour le transfert de fichier.
     * 
     * @param remoteUser
     * @param portClient
     * @param filn
     * @throws IOException
     */
    public FileReceiver(String remoteUser, int portClient,String filn) throws IOException {
                // TODO Auto-generated constructor stub                 
                this.port = 2015;
                this.ip = remoteUser;
                this.filname=filn;
                System.out.println("fill "+filname );   
                chatnet=new ChatNI(); 
                start();
        }

        @Override
        public void run() {
            int bytesRead; 
            int current=0;
            try {
                this.clientSocket = new Socket(ip,port);
            } catch (IOException ex) {
                Logger.getLogger(FileReceiver.class.getName()).log(Level.SEVERE, null, ex);
            }
            while(!clientSocket.isClosed()){
                
                System.out.println("sock "+ clientSocket);
                
                
                FileOutputStream fos = null;
                BufferedOutputStream bos = null;
                try {
                    byte[] mybytearray = new byte[4096];
                    this.reader = clientSocket.getInputStream();
                    
                    String path = System.getProperty("user.dir" );
                    System.out.println("chemin"+path);
                    fos = new FileOutputStream(path+"/"+filname);
                    while ((bytesRead = reader.read(mybytearray))> -1) {
                        System.out.println("byteread "+ bytesRead);
                        fos.write(mybytearray, 0,mybytearray.length);
                       
                    }
                    fos.flush();
                    clientSocket.close();
                    clientSocket.close();
                    FilePart fp=new FilePart(filname, mybytearray, true);
                    chatnet.Notifmsgrecu(fp);
                    
                    System.out.println("Terminer");
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(FileReceiver.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(FileReceiver.class.getName()).log(Level.SEVERE, null, ex);
                    try { reader.close();
                   
                    } catch (IOException ex1) {
                        Logger.getLogger(FileReceiver.class.getName()).log(Level.SEVERE, null, ex1);
                    }
                }finally {
                    try {
                        if (fos != null) {
                            fos.close();
                        }
                        
                        if (clientSocket != null) {
                            clientSocket.close();
                        }
                    }   catch (IOException ex) {
                        Logger.getLogger(FileReceiver.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    
                }

            }
        }
}